"use strict";
require("test").setup();

var Coroutine = require('coroutine');
var hash = require('hash');

var str_prefix = "ZG";

describe("user", function () {
    before(function () {

    });

    after(function () {
        clearDB();
    });


    it("登录", function () {
        httpPost("GET", "/v1.0/api/user/Sync", {})

        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/WebLogin", {
            "phone": "18888888888",
            "password": hash.md5(hash.md5("ZHYQ2022").digest().hex() + t).digest().hex(),
            "t": t,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 2);
        assert.equal(result.data.role, "超级管理员");
        assert.equal(result.data.edited_password, true);
        assert.equal(result.data.department, "超级管理");
    });
    //
    xit("首次修改密码", function () {
        var result = httpPost("POST", "/v1.0/api/user/EditPassword", {
            "old_password": hash.md5("ZHYQ2022").digest().hex(),
            "new_password": hash.md5("ZHYQ2022").digest().hex(),
        })
        assert.equal(result.code, "400");
        assert.equal(result.msg, "为保障账号安全，请勿使用旧密码");

        result = httpPost("POST", "/v1.0/api/user/EditPassword", {
            "old_password": hash.md5("ZHYQ2022").digest().hex(),
            "new_password": hash.md5("test123456").digest().hex(),
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
    });
    //@@TODO 首次修改密码会下线
    it("创建职工", function () {
        var result = httpPost("POST", "/v1.0/api/user/Create", {
            "name": "测试用户",
            "gender": "男",
            "phone": "18888888888",
            "role": "职工",
            "department": "外科",
        })
        assert.equal(result.code, "400");
        assert.equal(result.msg, "手机号已经注册");

        var result2 = httpPost("POST", "/v1.0/api/user/Create", {
            "name": "测试二",
            "gender": "男",
            "phone": "18300000001",
            "identity":"职工",
            "role": "职工",
            "department": "脑科",
        })
        assert.equal(result2.code, "200");
    });


    it("职工列表", function () {
        var result = httpPost("POST", "/v1.0/api/user/List", {
            "page": 1,
            "limit": 10,
            // "identity":"职工",
             "role":"职工"
        })
        console.error("职工列表  : ",result)
        assert.equal(result.code, "200");
        // assert.equal(result.data.list.length, 5);

        result = httpPost("POST", "/v1.0/api/user/List", {
            "name": "不存在",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);
    });
    // process.exit(0)
    it("编辑", function () {
        var result = httpPost("POST", "/v1.0/api/user/Update", {
            "id": 6,
            "role": "职工",
            "name": "张三"
        })
        assert.equal(result.code, "200");
    });

    it("患陪列表", function () {
        var result = httpPost("POST", "/v1.0/api/user/AccompanyList", {
            "role":"患陪",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);

        result = httpPost("POST", "/v1.0/api/user/AccompanyList", {
            "name": "不存在",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);
    });

    it("编辑用户", function () {
        var result = httpPost("POST", "/v1.0/api/user/Update", {
            "id": 10,
            "name": "测试用户二",
            "gender": "女",
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
    });

    it("冻结用户", function () {
        var result = httpPost("POST", "/v1.0/api/user/Update", {
            "id": 10,
            "status": "冻结中",
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
    });

    it("用户详情", function () {
        var result = httpPost("POST", "/v1.0/api/user/Get", {
            id: 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 10);
        assert.equal(result.data.name, "测试用户二");
        assert.equal(result.data.gender, "女");
        assert.equal(result.data.status, "冻结中");
        assert.equal(result.data.password, undefined);
    });

    it("启用用户", function () {
        var result = httpPost("POST", "/v1.0/api/user/Update", {
            "id": 10,
            "status": "启用中",
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");

        var result1 = httpPost("POST", "/v1.0/api/user/Get", {
            "id": 10,
        })
        assert.equal(result1.code, "200");
        assert.equal(result1.data.id, 10);
        assert.equal(result1.data.status, "启用中");

    });

    it("创建第三方职工", function () {
        var result = httpPost("POST", "/v1.0/api/user/Create", {
            "name": "张职工",
            "gender": "男",
            "phone": "18989898989",
            "role": "第三方职工",
            "identity":"第三方职工",
            "department": "第三方部门",
        })
        assert.equal(result.code, "200");

        var result2 = httpPost("POST", "/v1.0/api/user/Create", {
            "name": "李职工",
            "gender": "男",
            "phone": "18309000001",
            "role": "第三方职工",
            "department": "第三方部门",
        })
        assert.equal(result2.code, "200");
    });


    it("第三方职工列表", function () {
        var result = httpPost("POST", "/v1.0/api/user/AccompanyList", {
            "role":"第三方职工",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);

        result = httpPost("POST", "/v1.0/api/user/AccompanyList", {
            "name": "不存在",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);
    });

    it("第三方职工查询", function () {
        var result = httpPost("POST", "/v1.0/api/user/AccompanyList", {
            "role":"第三方职工",
            "name":"李",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
        assert.equal(result.data.list[0].name, "李职工");

        var result1 = httpPost("POST", "/v1.0/api/user/AccompanyList", {
            "role":"第三方职工",
            "name":"吴",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result1.code, "200");
        assert.equal(result1.data.list.length, 0);
    });

    it("冻结第三方职工", function () {
        var result = httpPost("POST", "/v1.0/api/user/Update", {
            "id": 12,
            "status": "冻结中",
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
    });

    it("第三方职工编辑", function () {
        var result = httpPost("POST", "/v1.0/api/user/Update", {
            "id": 12,
            "role": "第三方职工",
            "name": "张老三"
        })
        assert.equal(result.code, "200");
    });

    it("用户登出", function () {
        var result = httpPost("POST", "/v1.0/api/user/Logout", {})
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");

        var result1 = httpPost("POST", "/v1.0/api/user/Get", {
            "id": 2,
        })

        assert.equal(result1.code, "401");
        assert.equal(result1.msg, "用户未登录");
    });

    it("科室列表", function () {
        var result = httpPost("POST", "/v1.0/api/user/GetDepartment", {
            "level":1,
            "department":"心血管内科",
        })
        assert.equal(result.code, "200");
    });

    it("再次修改密码,需要填写旧密码，并且修改密码成功后会下线", function () {
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/WebLogin", {
            "phone": "18888888888",
            "password": hash.md5(hash.md5("ZHYQ2022").digest().hex() + t).digest().hex(),
            "t": t,
        })
        assert.equal(result.code, "200");

        var result1 = httpPost("POST", "/v1.0/api/user/EditPassword", {
            "new_password": hash.md5("test123456").digest().hex(),
        })
        assert.equal(result1.code, "400");
        assert.equal(result1.msg, "旧密码错误，请重新输入");

        var result2 = httpPost("POST", "/v1.0/api/user/EditPassword", {
            "old_password": hash.md5("ZHYQ2022").digest().hex(),
            "new_password": hash.md5("ZHYQ2022").digest().hex(),
        })
        assert.equal(result2.code, "400");
        assert.equal(result2.msg, "为保障账号安全，请勿使用旧密码");


        var result3 = httpPost("POST", "/v1.0/api/user/EditPassword", {
            "old_password": hash.md5("ZHYQ2022").digest().hex(),
            "new_password": hash.md5("test888888").digest().hex(),
        })
        assert.equal(result3.code, "200");
        assert.equal(result3.msg, "success");

        var result4 = httpPost("POST", "/v1.0/api/user/Get", {
            "id": 2,
        })
        assert.equal(result4.code, "401");
        assert.equal(result4.msg, "用户未登录");
    });
 //
    xit("重置密码-下线", function () {
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/WebLogin", {
            "phone": "18888888888",
            "password": hash.md5(hash.md5("test888888").digest().hex() + t).digest().hex(),
            "t": t,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.edited_password, 1);

        result = httpPost("POST", "/v1.0/api/user/ResetPassword", {
            "id": 2,
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");

        var result = httpPost("POST", "/v1.0/api/user/Get", {
            "id": 2,
        })
        assert.equal(result.code, "401");
        assert.equal(result.msg, "用户未登录");
    })
    //
    xit("h5-职工登录：1.sync - 首次进入h5页面需要去获取微信授权", function () {
        var result = httpPost("GET", "/v1.0/api/user/Sync?code=thisistestcode", {})
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 0);
        assert.equal(result.data.name, "");
        assert.equal(result.data.role, "");
        assert.equal(result.data.open_id, "123");
    })
    it("h5-职工登录角色不同：2.login", function () {
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/H5Login", {
            "phone": "18300000001",
            "password": hash.md5(hash.md5("ZHYQ2022").digest().hex() + t).digest().hex(),
            "t": t,
            "role": "管理员",
        })
        assert.equal(result.code, "400");
        assert.equal(result.msg, "角色认证错误");
    })

    it("h5-职工登录角色不同：2.login", function () {
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/H5Login", {
            "phone": "18300000001",
            "password": hash.md5(hash.md5("ZHYQ2022").digest().hex() + t).digest().hex(),
            "t": t,
            "identity":"第三方职工",
            "role": "职工",
        })
        assert.equal(result.code, "400");
    })


    it("h5-职工登录：2.login", function () {
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/H5Login", {
            "phone": "18300000001",
            "password": hash.md5(hash.md5("ZHYQ2022").digest().hex() + t).digest().hex(),
            "t": t,
            "identity":"职工",
            "role": "职工",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 10);
        assert.equal(result.data.name, "测试用户二");
        assert.equal(result.data.role, "职工");

        var result = httpPost("POST", "/v1.0/api/user/Get", {
            "id": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 10);
    })

    it("h5-职工登录：3.Sync 免密码登录", function () {
        var result = httpPost("GET", "/v1.0/api/user/Sync", {})
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 10);
        assert.equal(result.data.name, "测试用户二");
        assert.equal(result.data.role, "职工");
    })
    //
    xit("h5-职工登录：3.login 免密码登录", function () {
        var result = httpPost("POST", "/v1.0/api/user/H5Login", {
            "role": "职工"
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, undefined);
        assert.equal(result.data.name, "测试用户二");
        assert.equal(result.data.role, "职工");
    })
    //
    xit("h5-职工登录：4.logout 登出", function () {
        var result = httpPost("POST", "/v1.0/api/user/Logout", {})
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
    })

    it("h5-患者注册", function () {
        var result = httpPost("POST", "/v1.0/api/user/H5Register", {
            "role":"患陪",
            "name":"张患者",
            "gender":"男",
            "phone":"15678123491",
            "password":"ZHYQ2021",
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
    })

    it("H5职工列表", function () {
        var result = httpPost("POST", "/v1.0/api/user/H5List", {
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
    });

    it("H5职工编辑", function () {
        var result = httpPost("POST", "/v1.0/api/user/H5Update", {
           "id":10,
           "name":"王二麻子",
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
    });

    it("H5登出", function () {
        var result = httpPost("POST", "/v1.0/api/user/H5Logout", {})
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");

        var result1 = httpPost("POST", "/v1.0/api/user/H5Get", {
            "id": 10,
        })

        assert.equal(result1.code, "401");
        assert.equal(result1.msg, "用户未登录");
    });

});

require('test').run();